<?php

class Paging {
	var $db;
 	var $smarty;
 
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
		
	public function __destruct() {
		//
	}
	
	public function do_paging($countall, $data_array, $rows_per_page, $url, $pathname,$path) {
		$page = isset ( $_GET[$path] ) ? intval ($_GET[$path]) : 1;
		$links = $url;
		$path = $_GET[$path];
		$testpage ="";
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
		$number_of_page =  ceil($countall/$rows_per_page);
		$maxPage = $number_of_page;
		$small = $page-3;
		$large = $page+3;
		$khoangfirst = $number_of_page - "6";
		$numsendpage = $number_of_page - "3";
		$numcountobj = "";
		if ($number_of_page > 1) {
			$testpage ="1";
		}
		if ($number_of_page > 0) {
			$list_page = "";
			if( $number_of_page <= 10 ) {
				for ($i = 1; $i <= $number_of_page; $i++) {
					if($i == $number_of_page)
						$symbol = "&nbsp;";
					else
						$symbol = "&nbsp;";
					if($i == $page) {
						$list_page .= "<b class='paging_activepage'> {$i} </b> {$symbol} ";
						$total = "<b>{$i}</b> - <b> {$number_of_page}</b>";
						$numcountobj = $i;
					} else {
						$list_page .= "<a href='$links".$pathname."=".$i."' >{$i}</a> {$symbol} ";	
					}
				}
			} else {
				for ($i = 1; $i <= $number_of_page; $i++) {
					if($i == $number_of_page)
						$symbol = "&nbsp;";
					else
						$symbol = "&nbsp;";
					if($i == $page){
						$numcountobj = $i;
						$show_page .= "<b class='pink'> {$i} </b> {$symbol}";
						$total = "<b>{$i}</b> - <b> {$number_of_page}</b>";
					}
					if($i<$large && $i>$small && $i != $page){
						$show_page .= "<a href='$links".$pathname."=".$i."' >{$i}</a> {$symbol} ";
					}
					if($i <=3){
						$list_first .= "<a href='$links".$pathname."=".$i."' >{$i}</a> {$symbol} ";
					}
					if($i > $numsendpage){
						$list_last .= "<a href='$links".$pathname."=".$i."' >{$i}</a> {$symbol} ";
					}
				}
				
				if($page <6){
					$list_page = $show_page." ... ".$list_last;
				}
				if($page >$khoangfirst){
					$list_page = $list_first." ... ".$show_page;
				}
				if($page >=6 && $page<=$khoangfirst) {
					$list_page = $list_first." ... ".$show_page." ... ".$list_last;
				}
			}
			$strlistpage = Paging::get_select_box($number_of_page, $path,$page,$pathname,$links);
		}
		$pre_labels = Paging::get_previous_label($page,$pathname,$links);
		$next_labels = Paging::get_next_label($maxPage,$page,$pathname,$links);
		
		if($numcountobj < $number_of_page) {
			$toobject = $rows_per_page * $numcountobj; 
			$fromobject = $toobject - $rows_per_page +1; 
		} else {
			$toobject = $countall;
			$fromobject = (($numcountobj -1) * $rows_per_page) +1; 		
		}
		$strobj = "<b>{$fromobject}</b> - <b>$toobject</b>";
		$data_array['TESTPAGE']=$testpage;
		$data_array['LISTPAGE']=$list_page;
		$data_array['PRE']=$pre_labels['PRE'];
		$data_array['FIRST']=$pre_labels['FIRST'];
		$data_array['NEXT']=$next_labels['NEXT'];
		$data_array['LAST']=$next_labels['LAST'];
		$data_array['TOTAL']=$strobj;
		$data_array['TOTALPAGE']=$number_of_page;
		$data_array['TOTALDATA']=$countall;
		$data_array['COMBOPAGE']=$strlistpage;
		$data_array['LINK']= $links;
		$data_array['PATHNAME']=$pathname;
		$data_array['DATASHAREPAGE']=$data_sharepage['data'];
		return $data_array;
	}
	
	//return Array (el NEXT and LAST)
	private function get_next_label($maxPage,$page,$pathname,$links) {
		$labels = array();
		if ($page  < $maxPage){
			$nextpage = $page  + 1;
			$next = " <a href='$links".$pathname."=".$nextpage."' >" . "Next" . "</a> ";
			$last = " <a href='$links".$pathname."=".$maxPage."' >&raquo;</a> ";
		} else {
			$next = '';//we're on the last page, don't enable 'next' link
			$last = '';//nor 'last page' link
		}
		$labels['NEXT'] = $next;
		$labels['LAST'] = $last;
		return $labels;
	}
	
	//return Array (el PRE and FIRST)
	private function get_previous_label($page,$pathname,$links) {
		$labels = array();
		if ($page  > 1) {
			$lastpage = $page  - 1;
			$prev = " <a href='$links".$pathname."=".$lastpage."' >" . "Previous" . "</a> ";
			$first = " <a href='$links".$pathname."=1' >&laquo;</a> ";
		} else {
			$prev  = '';//we're on page one, don't enable 'previous' link
			$first = '';//nor 'first page' link
		}
		$labels['PRE'] = $prev;
		$labels['FIRST'] = $first;
		return $labels;
	}
	
	private function get_select_box($number_of_page, $path,$page,$pathname,$links) {
		$strlistpage = "";
		for($k=1;$k<=$number_of_page;$k++) {
			if($path == $k)
				$strlistpage .= "<option value='".$k."' selected><a href='$links".$pathname."=".$k."' >{$k}</a></option>";
			else
				$strlistpage .= "<option value='".$k."'><a href='$links".$pathname."=".$k."' >{$k}</a></option>";
		}
		return $strlistpage;
	}
}
?>